package com.udaos.modules.company.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.company.Ud_company_school;
import com.udaos.modules.company.services.UdCompanySchoolService;


/**
 * 主体-驾校WEB层
 * @author mutaotao
 * @time 2016-11-07 17:19:18
 * 
 */
@IocBean
@At("/platform/companySchool")
@Filters({ @By(type = PrivateFilter.class)})
public class UdCompanySchoolController {
	
	@Inject
	private UdCompanySchoolService udCompanySchoolService;


	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/base/companyschool/index.html")
	@RequiresAuthentication
	public void index() {

	}
	/**
	 * 加载数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		return udCompanySchoolService.init(length, start, draw);
	}
	
	/**
	 * 新增
	 * @param unitid
	 * @return
	 */
	@At
	@Ok("beetl:/platform/base/companyschool/add.html")
	@RequiresAuthentication
	public Object add() {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("companys", udCompanySchoolService.findCompanys());
		return map;
	}
	
	/**
	 * 新增
	 * @param company
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object addDo(@Param("..") Ud_company_school company_school,@Param("isDefault") String isDefault,HttpServletRequest req) {
		try {
			udCompanySchoolService.save(company_school,isDefault);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	/*@At("/edit/?")
	@Ok("beetl:/platform/base/companyregion/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("companyName", udCompanySchoolService.getCompanyName(udCompanySchoolService.fetch(id).getCompany_id()));
		map.put("regions", udCompanySchoolService.findRegions(udCompanySchoolService.fetch(id).getCompany_id()));
		map.put("company_school", udCompanySchoolService.fetch(id));
		map.put("defaultSchool", udCompanySchoolService.getDefaultSchool(udCompanySchoolService.fetch(id).getCompany_id(),udCompanySchoolService.fetch(id).getRegion_id()));
		return map;
	}*/
	
	/**
	 * 修改
	 * @param member
	 * @param req
	 * @return
	 */
	/*@At
	@Ok("json")
	@RequiresAuthentication
	public Object editDo(@Param("..") Ud_company_school company_school,@Param("isDefault") String isDefault, HttpServletRequest req) {
		try {
			udCompanySchoolService.updateCr(company_school,isDefault);
	   		return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}*/
	
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_company_school company_school = udCompanySchoolService.fetch(id);
        	company_school.setDelFlag(true);
        	udCompanySchoolService.updateIgnoreNull(company_school);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
	/**
	 * 获取该主体下开通的片区
	 * @param company_id
	 * @return
	 */
	@At
    @Ok("json")
	public Object findRegions(String company_id){
		return udCompanySchoolService.findRegions(company_id);
	}
	/**
	 * 获取该主体下开通的片区的驾校
	 * @param company_id
	 * @return
	 */
	@At
	@Ok("json")
	public Object chooseSchools(String region_id){
		return udCompanySchoolService.chooseSchools(region_id);
	}

}